<template>
  <div class="home">
    <!-- 导航条 -->
    <van-nav-bar fixed>
      <template #left>
        <div class="date-one">
          <div class="text-two">{{ specificDate.day }}</div>
          <div class="text-three">{{ specificDate.monthOne }}月</div>
        </div>
        <div class="text-one">知乎日报</div>
      </template>
      <template #right>
        <van-icon name="manager" size="30" color="#000" @click="SignIn()" />
      </template>
    </van-nav-bar>
    <div class="fei"></div>
    <!-- 刷新 -->
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <!-- 轮播图 -->
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item
          v-for="(img, index) in storageTwo"
          :key="index"
          @click="DetailsPage(img.id)"
        >
          <img :src="img.image" alt="" class="imges" />
          <div class="img-text">
            <div class="img-text-one">{{ img.title }}</div>
            <div class="img-text-two">{{ img.hint }}</div>
          </div>
        </van-swipe-item>
      </van-swipe>
      <div
        class="content"
        v-for="(itme, index) in storageOne"
        :key="index"
        @click="DetailsPage(itme.id)"
      >
        <div class="content-img">
          <img :src="itme.images" alt="" />
        </div>
        <div class="content-test">
          <div class="content-text-one">{{ itme.title }}</div>
          <div class="content-text-two">{{ itme.hint }}</div>
        </div>
      </div>
      <!-- 加载旧数据 -->
      <div class="content-kuang" v-for="(itme, index) in bigData" :key="index">
        <div class="wenzi">
          {{ bigDatanumber[index] }}
          <hr />
        </div>
        <div
          class="content"
          v-for="(itmes, indexOne) in bigData[index].stories"
          :key="indexOne"
          @click="DetailsPage(itmes.id)"
        >
          <div class="content-img">
            <img :src="itmes.images" alt="" />
          </div>
          <div class="content-test">
            <div class="content-text-one">{{ itmes.title }}</div>
            <div class="content-text-two">{{ itmes.hint }}</div>
          </div>
        </div>
      </div>
      <!-- 触底加载 -->
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        offset="50"
      >
        <hr />
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script>
import { Toast } from "vant";
export default {
  name: "Home",
  data() {
    return {
      isLoading: false,
      storageOne: [],
      storageTwo: [],
      storagedata: "",
      shijianchuo: "",
      specificDate: {
        number: 0,
        numberOne: 0,
        year: "",
        month: "",
        monthOne: "",
        day: "",
      },
      current: 0,
      loading: false,
      finished: false,
      bigData: [],
      bigDatanumber: [],
    };
  },
  created() {
    this.getHotProductOne();
    Toast.loading({
      message: "加载中...",
      forbidClick: true,
    });
    let a = Date.now();
    this.shijianchuo = a;
  },
  methods: {
    //刷新
    onRefresh() {
      setTimeout(() => {
        Toast("刷新成功");
        this.isLoading = false;
        this.bigDatanumber = [];
        this.storageOne = [];
        this.bigData = [];
        this.storageTwo = [];
        this.getHotProductOne();
      }, 1000);
    },
    //获取数据
    getHotProductOne() {
      //发起请求
      this.axios({
        //请求类型
        method: "get",
        //请求路径
        url: "https://apis.netstart.cn/zhihudaily/stories/latest",
        //如果get请求, 请求参数需要写在params字段中
        // params: {
        //   page: this.shu,
        // },
      })
        .then((result) => {
          // console.log(result);
          //存储数据
          this.storageOne = result.data.stories;
          this.storageTwo = result.data.top_stories;

          this.storagedata = result.data.date;
          this.specificDate.number = Number(this.storagedata);
          this.specificDate.numberOne = this.specificDate.number + 1;
          this.specificDate.year = this.storagedata.slice(0, 4);
          this.specificDate.month = this.storagedata.slice(4, 6);
          this.specificDate.monthOne = this.storagedata.slice(5, 6);
          this.specificDate.day = this.storagedata.slice(6, 8);
          let a = Date.now();
          this.shijianchuo = a;
        })
        .catch((err) => {});
    },
    getHotProducttwo(shu) {
      // console.log(shu);
      this.axios({
        method: "get",
        url: "https://apis.netstart.cn/zhihudaily/stories/before/" + shu,
      })
        .then((result) => {
          this.bigData.push(result.data);
          this.bigDatanumber = [];
          for (let i = 0; i < this.bigData.length; i++) {
            let shijian = this.bigData[i].date;
            let year = shijian.slice(0, 4);
            let month = shijian.slice(4, 6);
            let day = shijian.slice(6, 8);
            let wenzi = year + "年" + month + "月" + day + "日";
            this.bigDatanumber.push(wenzi);
          }
          // 加载状态结束
          this.loading = false;
        })
        .catch((err) => {});
    },
    //查看笔记详情页面
    DetailsPage(id) {
      this.$router.push({ name: "DetailPage", params: { id } });
    },
    SignIn() {
      this.$router.push({ name: "SignIn" });
    },
    //加载
    onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(() => {
        let shu = (this.shijianchuo = this.shijianchuo - 100000000);
        var myDate = new Date(shu);
        let Year = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
        let Month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
        let Dateone = myDate.getDate(); //获取当前日(1-31)
        Month = Month < 10 ? "0" + Month : Month;
        Dateone = Dateone < 10 ? "0" + Dateone : Dateone;
        shu = Year + Month + Dateone;
        shu = Number(shu);
        if (shu > 1000) {
          this.getHotProducttwo(shu);

          // 数据全部加载完成
          // if (this.list.length >= 40) {
          //   this.finished = true;
          // }
        } else {
          this.loading = false;
          return;
        }
      }, 1000);
    },
  },
};
</script>
<style lang="less" scoped>
::v-deep .van-nav-bar__text {
  color: black;
}
::v-deep .van-nav-bar .van-icon {
  color: black;
}
::v-deep .van-nav-bar {
  height: 65px;
}
.pois {
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
}
.imges {
  width: 100%;
  position: relative;
}
.img-text {
  width: 300px;
  height: 70px;
  position: absolute;
  bottom: 10px;
  left: 20px;
  .img-text-one {
    width: 100%;
    height: 50px;
    color: white;
    font-size: 18px;
  }
  .img-text-two {
    width: 100%;
    height: 20px;
    font-size: 12px;
    color: rgb(235, 235, 235);
    line-height: 20px;
  }
}
::v-deep .van-nav-bar__content {
  height: 65px;
}
.fei {
  width: 100%;
  height: 65px;
}
.date-one {
  width: 40px;
  height: 40px;
  border-right: 1px solid black;
  .text-two {
    font-size: 20px;
  }
}
.text-one {
  width: 110px;
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 20px;
}
.content {
  width: 100%;
  height: 100px;
  .content-img {
    float: right;
    width: 90px;
    height: 90px;
    padding: 5px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .content-test {
    width: 240px;
    height: 70px;
    padding: 15px 15px;
    .content-text-one {
      font-size: 15px;
    }
    .content-text-two {
      font-size: 11px;
    }
  }
}
.content-kuang {
  width: 100%;
  height: 100%;
  .wenzi {
    font-size: 13px;
    color: rgb(179, 179, 179);
    margin: 10px;
    hr {
      margin: 9px 0;
      float: right;
      width: 250px;
    }
  }
  .content {
    width: 100%;
    height: 100px;
    .content-img {
      float: right;
      width: 90px;
      height: 90px;
      padding: 5px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .content-test {
      width: 240px;
      height: 70px;
      padding: 15px 15px;
      .content-text-one {
        font-size: 15px;
      }
      .content-text-two {
        font-size: 11px;
      }
    }
  }
}
</style>